package com.battcn.platform.controller.sys;

import com.battcn.framework.common.annotation.BattcnLog;
import com.battcn.framework.mybatis.page.DataGrid;
import com.battcn.platform.controller.BaseController;
import com.battcn.platform.pojo.message.ApiResult;
import com.battcn.platform.pojo.po.sys.Manager;
import com.battcn.platform.pojo.po.sys.Menu;
import com.battcn.platform.pojo.po.sys.Office;
import com.battcn.platform.service.sys.ManagerService;
import com.battcn.platform.service.sys.MenuService;
import com.battcn.platform.service.sys.OfficeService;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

import static com.battcn.framework.common.exception.BattcnException.notFound;


/**
 * @author Levin
 */
@Controller
@RequestMapping("/sys/office")
@Api(value = "部门管理")
@ApiIgnore
public class OfficeController extends BaseController {

    private final OfficeService officeService;
    private final ManagerService managerService;

    @Autowired
    public OfficeController(OfficeService officeService,ManagerService managerService) {
        this.officeService = officeService;
        this.managerService = managerService;
    }

    @GetMapping("/list")
    public String list(DataGrid grid) {
        PageInfo<Office> offices = this.officeService.listOfficeForDataGrid(grid);
        request.setAttribute("page", this.officeService.listOfficeForDataGrid(grid));
        return "sys/office/list";
    }

    @GetMapping(value = "/edit")
    public String edit(Integer id) {
        if (id != null) {
            request.setAttribute("dto", this.officeService.selectById(id).orElseThrow(() -> notFound("该数据已失效")));
            Manager manager = new Manager();
            manager.setOffice(id);
            request.setAttribute("userList", this.managerService.listManager(manager));
        }
        request.setAttribute("menus", this.officeService.listOffice());
        return "sys/office/edit";
    }

    @BattcnLog(module = "部门管理", methods = "保存部门", description = "添加/修改部门信息")
    @PostMapping(value = "/save")
    @ResponseBody
    public ApiResult<String> save(Office office) {
        if (office != null) {
            this.officeService.saveOrUpdate(office);
        }
        return ApiResult.SUCCESS;
    }

    @BattcnLog(module = "部门管理", methods = "部门菜单", description = "删除部门信息")
    @PostMapping(value = "/remove")
    @ResponseBody
    public ApiResult<String> del(Integer[] ids) {
        Lists.newArrayList(ids).forEach(this.officeService::deleteById);
        return ApiResult.SUCCESS;
    }


}
